package fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.qianfeng.lingshijia.R;

import adapter.DetailTypeAdapater;
import entity.Details;
import utils.Constants;
import utils.DownloadUtils;

/**
 * 商品详情下规格参数界面
 */
public class TypeFragment extends BaseFragment {
    private int productId;

    private ListView listView;

    private DetailTypeAdapater detailTypeAdapater;

    public void setProductId(int productId){
        this.productId = productId;

    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,  Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view  = inflater.inflate(R.layout.fragment_type, container, false);
        return view;
    }

    @Override
    public void init(View view) {
        listView = (ListView)view.findViewById(R.id.id_lv_detailtype);
        detailTypeAdapater = new DetailTypeAdapater(getActivity());
        listView.setAdapter(detailTypeAdapater);
    }

    @Override
    public void loadDatas() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("productId",productId+"");
        DownloadUtils.postJson(Constants.URL.DETAIL_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;
                if (response != null) {
                    Gson gson = new Gson();
                    Details details = gson.fromJson(response, Details.class);
                    detailTypeAdapater.setData(details.getData().getSpec());

                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        }, params);
        super.loadDatas();
    }
}
